import axios from "axios";
import settle from 'axios/lib/core/settle'
import buildURL  from 'axios/lib/helpers/buildURL';
import combineURLs from 'axios/lib/helpers/combineURLs'
import mode from '@/mode.js'
axios.defaults.adapter = function(config) { //自己定义个适配器，用来适配uniapp的语法
    return new Promise((resolve, reject) => {
        uni.request({
            method: config.method.toUpperCase(),
            url: combineURLs(config.baseURL , buildURL(config.url, config.params, config.paramsSerializer)),
            header: config.headers,
            data: config.data,
            dataType: config.dataType,
            responseType: config.responseType,
            sslVerify: config.sslVerify,
            complete: function complete(response) {
                response = {
                    data: response.data,
                    status: response.statusCode,
                    errMsg: response.errMsg,
                    header: response.header,
                    config: config
                };

                settle(resolve, reject, response);
            }
        })
    })
}
export class GuiguRequest{

     instance

     defaultRequestInterceptor={
        requestInterceptor:(config)=>{
            return config
        },
        requestInterceptorCatch:(error)=>{
            return Promise.reject({msg:error.message})
        }
    }

     defaultResponseInterceptor={
        responseInterceptor:response=>{
            const result = response.data
            //如果后端返回错误状态码
            if(result.code===401){
                return Promise.reject(result)
            }

            if(result.code!==200){
                return Promise.reject(result)
            }

            //正常返回
            return response.data
        },

        responseInterceptorCatch:error=>{
			console.log(error)
			if(error.response){
				  return Promise.reject(error.response.data?error.response.data:{msg:error.message})
			}
            return Promise.reject({msg:error.message})
        }
    }

    
    defaultSuccessFn(data){
        return data
    }

    defaultErrorFn(error){
        return Promise.reject(error)
    }
    constructor(config){
        this.instance=axios.create(config)
        this.instance.interceptors.request.use(
            this.defaultRequestInterceptor?.requestInterceptor,
            this.defaultRequestInterceptor?.requestInterceptorCatch
            )
        this.instance.interceptors.response.use(
                this.defaultResponseInterceptor.responseInterceptor,
                this.defaultResponseInterceptor.responseInterceptorCatch
            )
    }
    static  create(config){
        return new GuiguRequest(config)
    }

    upload(config){

       return this.post({
           
		   'url':config.url,
		   'data':config.data,
		   'params':config.params,
		    headers: {
		       'Content-Type': 'multipart/form-data'
		     }
		   
       })
        .then(this.defaultSuccessFn)
        .catch(this.defaultErrorFn)
    }

    post(config){
        config.method="post"
        return this.request(config)
    }

    get(config){
        config.method="get"
        return this.request(config)
    }

    put(config){
    
        config.method='put'
        return this.request(config)
    }

    delete(config){
        config.method='delete'
        return this.request(config)
    }
    request(config){
        return this.instance.request(config)
        .then(this.defaultSuccessFn)
        .catch(this.defaultErrorFn)
    }
}



const config = {
    baseURL: mode.APP_BASE_URL,
    timeout: 5000
}

const baseRequest=GuiguRequest.create(config)

export default baseRequest


